from crawler.logger import monitor_logger


class MonitorDownloaderMiddleware(object):

    def process_response(self, request, response, spider):
        task = request.meta['task']
        monitor_logger.info({
            **task,
            'service': spider.name,
            'logLevel': 'INFO',
            'crawlerStep': 'request_end',
            'status': response.status,
            'proxyType': request.meta.get('monitor_proxy_type', 'local'),
            'message': 'success',
            'other': request.url,
        })
        return response

    def process_exception(self, request, exception, spider):
        task = request.meta['task']
        monitor_logger.info({
            **task,
            'service': spider.name,
            'logLevel': 'ERROR',
            'crawlerStep': 'request_exception',
            'proxyType': request.meta.get('monitor_proxy_type', 'local'),
            'status': 500,
            'message': exception,
            'other': request.url,
        })
